package com.myapp.ruyixing.ui;

import static android.view.View.GONE;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.myapp.ruyixing.Config;
import com.myapp.ruyixing.R;
import com.myapp.ruyixing.ui.shopmanager.ShopManagerMainFragment;
import com.myapp.ruyixing.utils.LocationUtils;

public class MainActivity extends BaseActivity implements OnClickListener,
		IShowLoading, BDLocationListener {

	// ArrayList<OnLocationChangedListener> mListeners = new
	// ArrayList<MainActivity.OnLocationChangedListener>();
	// final static Object lock = new Object();

	View mSearchView;
	View mRegistrationView;
	View mMemberView;
	View mMoreView;
	View mUserView;

	Fragment mMemberFragment;
	Fragment mUserFragment;
	Fragment mMoreFragment;
	Fragment mSigninFragment;
	Fragment mSearchFragment;

	int currSelectedTabViewId;

	boolean mGoingToExit = false;

	LocationClient mLocationClient;

	Handler mHandler = new Handler() {
		public void handleMessage(Message msg) {
			mGoingToExit = false;
		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		mSearchView = findViewById(R.id.layout_search);
		mSearchView.setOnClickListener(this);
		mRegistrationView = findViewById(R.id.layout_registration);
		mRegistrationView.setOnClickListener(this);
		mMemberView = findViewById(R.id.layout_member);
		mMemberView.setOnClickListener(this);
		mMoreView = findViewById(R.id.layout_more);
		mMoreView.setOnClickListener(this);
		mUserView = findViewById(R.id.layout_user);
		mUserView.setOnClickListener(this);

		mSearchView.performClick();
		findViewById(R.id.layout_loading).setOnClickListener(this);

		mLocationClient = new LocationClient(this,
				LocationUtils.getDefaultOption());
		mLocationClient.registerLocationListener(this);

		if (!Config.sLocated) {
			if (LocationUtils.isGpsAvailable(this)) {
				locateMyPosition();
			}
		}
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		if (currSelectedTabViewId == id) {
			return;
		}
		currSelectedTabViewId = id;

		switch (id) {
		case R.id.layout_loading:
			break;
		case R.id.layout_registration:
			if (mRegistrationView.isSelected()) {
				return;
			}
			mRegistrationView.setSelected(true);
			mMemberView.setSelected(false);
			mMoreView.setSelected(false);
			mSearchView.setSelected(false);
			mUserView.setSelected(false);

			if (mSigninFragment == null) {
				mSigninFragment = new SigninFragment();
			}
			showFragment(mSigninFragment);
			break;

		case R.id.layout_member:
			if (mMemberView.isSelected()) {
				return;
			}
			mRegistrationView.setSelected(false);
			mMemberView.setSelected(true);
			mMoreView.setSelected(false);
			mSearchView.setSelected(false);
			mUserView.setSelected(false);

			if (mMemberFragment == null) {
				mMemberFragment = new MemberFragment();
			}
			showFragment(mMemberFragment);
			break;

		case R.id.layout_more:
			if (mMoreView.isSelected()) {
				return;
			}
			mRegistrationView.setSelected(false);
			mMemberView.setSelected(false);
			mMoreView.setSelected(true);
			mSearchView.setSelected(false);
			mUserView.setSelected(false);

			if (mMoreFragment == null) {
				mMoreFragment = new MoreFragment();
			}
			showFragment(mMoreFragment);
			break;

		case R.id.layout_search:
			if (mSearchView.isSelected()) {
				return;
			}
			mRegistrationView.setSelected(false);
			mMemberView.setSelected(false);
			mMoreView.setSelected(false);
			mSearchView.setSelected(true);
			mUserView.setSelected(false);

			if (mSearchFragment == null) {
				if (Config.isShopManagerVersion(this)) {
					mSearchFragment = new ShopManagerMainFragment();
				} else {
					mSearchFragment = new SearchFragment();
				}
			}
			showFragment(mSearchFragment);
			break;

		case R.id.layout_user:
			if (Config.isShopManagerVersionAndNotBinded(this)) {
				showToast("您未绑定会员账号，请绑定后再使用此功能！");
			} else {
				if (mUserView.isSelected()) {
					return;
				}
				mRegistrationView.setSelected(false);
				mMemberView.setSelected(false);
				mMoreView.setSelected(false);
				mSearchView.setSelected(false);
				mUserView.setSelected(true);

				if (mUserFragment == null) {
					mUserFragment = new UserFragment();
				}
				showFragment(mUserFragment);
			}
			break;

		default:
			break;
		}
	}

	private void showToast(String msg) {
		Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
	}

	private void showFragment(Fragment fragment) {
		if (fragment != null) {
			getSupportFragmentManager().beginTransaction()
					.replace(R.id.layout_content, fragment)
					.commitAllowingStateLoss();
		}
	}

	@Override
	public void onBackPressed() {
		if (mGoingToExit) {
			super.onBackPressed();
			System.exit(0);
		} else {
			mGoingToExit = true;
			Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
			mHandler.sendEmptyMessageDelayed(0, 2000);
		}
	}

	@Override
	protected void onPause() {
		mGoingToExit = false;
		mHandler.removeMessages(0);
		super.onPause();
	}

	@Override
	public void showLoading(boolean show, String msg) {
		if (isFinishing()) {
			return;
		}
		if (show) {
			findViewById(R.id.layout_loading).setVisibility(View.VISIBLE);
			((TextView) findViewById(R.id.text_loading)).setText(msg);
		} else {
			findViewById(R.id.layout_loading).setVisibility(GONE);
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	private void locateMyPosition() {
		if (!mLocationClient.isStarted()) {
			mLocationClient.start();
		}
		mLocationClient.requestLocation();
		showLoading(true, "定位中，请稍候！");
	}

	private void stopLocating() {
		mLocationClient.stop();
		showLoading(false, "");
	}

	// public void registerOnLocationChangedListener(
	// OnLocationChangedListener listener) {
	// if (listener == null) {
	// return;
	// }
	// synchronized (lock) {
	// mListeners.remove(listener);
	// mListeners.add(listener);
	// }
	// }
	//
	// public void unregisterOnLocationChangedListener(
	// OnLocationChangedListener listener) {
	// if (listener == null) {
	// return;
	// }
	// synchronized (lock) {
	// mListeners.remove(listener);
	// }
	// }

	@Override
	public void onReceiveLocation(BDLocation location) {
		if (location == null) {
			return;
		}
		Config.sLocated = true;
		stopLocating();
		Config.sMyLocation = location;
		if (!CityListActivity.isCitySet(this)) {
			startActivity(new Intent(this, CityListActivity.class));
		}
		// synchronized (lock) {
		// for (int i = 0; i < mListeners.size(); i++) {
		// mListeners.get(i).onLocationChanged(location);
		// }
		// }
	}

	@Override
	public void onReceivePoi(BDLocation arg0) {
	}
	//
	// public static interface OnLocationChangedListener {
	// public void onLocationChanged(BDLocation location);
	// }

}
